<?php
class TagModel extends RelationModel{
	
	public function adds(){
		$tags = explode(" ", $_REQUEST["subject"]);
		$u_tags = $this->getUserTags();
		foreach($u_tags as $k=> $v){
			if(!in_array($v['subject'], $tags)) $this->del($v['subject']);
		}
		foreach($tags as $k=>$v){
			if(!trim($v)) continue ;
			$result = $this->checkTag($v);
			if(!$result) $return = $this->add($v);
		}
		return 1 ;
	}
	
	public function add($tag){
		$data['subject'] = $tag;
		$data['share_id'] = $_REQUEST["share_id"];
		$data['uid'] = Session::getLocal('id');
		$data['create_time'] = time();
		$data['status'] = 1;
		$result = M("Tag")->data($data)->add();
		return $result;
	}
	
	public function del($tag){
		$condition['subject'] = $tag;
		$condition['share_id'] = $_REQUEST["share_id"];
		$condition['uid'] = Session::getLocal('id');
		$result = M("Tag")->where($condition)->data(array("status"=>"-1"))->save();
		return $result;
	}
	
	public function checkTag($tag){
		$data['subject'] = $tag;
		$data['share_id'] = $_REQUEST["share_id"];
		$data['uid'] = Session::getLocal('id');
		$data['status'] = 1;
		$result = M("Tag")->where($data)->find();
		return $result;
		
	}
	
	public function getUserTags(){
    	$map_user = array(
    		"uid"      => Session::getLocal('id'),
    		"share_id" => $_REQUEST['share_id']
    	);
		$u_tags = M("Tag")->where($map_user)->select();
		return $u_tags;
	}
	
	
	
	
	
}
?>
